package net.lastcoder.ifserver.util;

import java.security.SecureRandom;
import java.util.Random;

public class TextUtils {
    public static final String TAG = "TextUtils";

    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**连接字符串，中间隔着split
     * @param strs 要组装的字符串
     * @param split 分割
     * @return 连接后的字符串
     */
    public static String concatStringNotEnd(String[] strs, String split) {
        String concatKeys = "";
        for (String key : strs) {
            concatKeys = concatKeys + key + split;
        }
        if (concatKeys.length() >= split.length()) {
            concatKeys = concatKeys.substring(0, concatKeys.length() - split.length());
        }

        return concatKeys;
    }

    /**连接字符串，中间隔着split
     * @param strs 要组装的字符串
     * @param split 分割
     * @return 连接后的字符串
     */
    public static String concatStringNotEnd(String[] strs, String tag, String split) {
        String concatKeys = "";
        for (String key : strs) {
            concatKeys = concatKeys + key + tag + split;
        }
        if (concatKeys.length() >= split.length()) {
            concatKeys = concatKeys.substring(0, concatKeys.length() - split.length());
        }

        return concatKeys;
    }

    /**连接字符串，中间隔着split
     * @param str 要组装的字符
     * @param split 分割
     * @return 连接后的字符串
     */
    public static String concatStringNotEnd(String str, int count, String split) {
        String concatKeys = "";
        for (int i = 0; i < count; i++) {
            concatKeys = concatKeys + str + split;
        }
        if (concatKeys.length() >= split.length()) {
            concatKeys = concatKeys.substring(0, concatKeys.length() - split.length());
        }

        return concatKeys;
    }

    /**strs1[0] + split1 + strs[2] + split2
     * @param strs1 要组装的字符1
     * @param strs2 要组装的字符2
     * @return 连接后的字符串
     */
    public static String concatStringMix(String[] strs1, String split1, String[] strs2, String split2) {
        String concatKeys = "";
        for (int i = 0; i < strs1.length; i++) {
            concatKeys = concatKeys + strs1[i] + split1 + strs2[i] + split2;
        }
        if (concatKeys.length() >= split2.length()) {
            concatKeys = concatKeys.substring(0, concatKeys.length() - split2.length());
        }

        return concatKeys;
    }

    private static char[] numbersAndDigits = ("123456789").toCharArray();
    private static Random randGen = new SecureRandom();
    public static String randomDigit(int length) {
        if (length < 1) {
            return null;
        }
        // Create a char buffer to put random letters and numbers in.
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbersAndDigits[randGen.nextInt(numbersAndDigits.length)];
        }
        return new String(randBuffer);
    }

    private static char[] lowChars = ("abcdefghijklmnopqrstuvwxyz").toCharArray();

    public static String randomLowChars(int length) {
        if (length < 1) {
            return null;
        }
        // Create a char buffer to put random letters and numbers in.
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = lowChars[randGen.nextInt(lowChars.length)];
        }
        return new String(randBuffer);
    }
}
